home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 476-500 / disk_484 / msclock / source / msclock.c < prev   
C/C++ Source or Header  |  1992-05-06  |  9KB  |  241 lines

  1. /*
  2.  *    MSClock.c        © by Martin Steppler 12.02.91
  3.  *
  4.  *    Yet another clock utility for use with L:MSClock-Handler
  5.  */
  6.  
  7. // includes
  8.  
  9. #include <stdio.h>
  10. #include <libraries/dos.h>
  11. #include <devices/timer.h>
  12. #include <devices/input.h>
  13. #include <intuition/intuition.h>
  14. #include <intuition/intuitionbase.h>
  15. #include <exec/exec.h>
  16. #include <functions.h>
  17.  
  18. //defines
  19.  
  20. #define SIG_CHANGE  SIGBREAKF_CTRL_C
  21. #define SIG_CLOSE   SIGBREAKF_CTRL_D
  22. #define CHILD_READY SIGBREAKF_CTRL_D
  23. #define EOS 0
  24. #define PORTNAME "Martin's Clock-Port"
  25. #define WriteStdOut(str) Write(StdOut, str, (LONG)strlen(str))
  26.  
  27. #define TIME_WIDTH         80
  28. #define DATE_WIDTH         112
  29. #define ONLINE_WIDTH       80
  30. #define MEM_WIDTH          152
  31. #define FRONTBACKGAD_WIDTH 62
  32.  
  33. // prototypes
  34.  
  35. long Chk_Abort() { return(0); }
  36. void main(int, char **);
  37. int GetWidth(int DefFlag);
  38. int GetOverscan(void);
  39.  
  40. // globals
  41.  
  42. struct TheClock
  43. {
  44.    struct MsgPort    Port;          // Global messageport.
  45.    BPTR              Segment;       // Pointer to handler segment.
  46.    LONG              SegSize;       // Size of TheClock structure.
  47.    struct Task      *Father;        // Calling process.
  48.    struct Task      *Child;         // Waiting process
  49.    int               LeftEdge;      // LeftEdge of our window
  50.    int               Width;         // Width of our window
  51.    int               Online;        // online-flag
  52.    int               Flags;         // display-flags
  53.    int               FlagsBackup;   // backup of the display-flags
  54.    int               Status;        // status-flag
  55. };
  56.  
  57. struct IntuitionBase *IntuitionBase;
  58. struct DosLibrary    *DosBase;
  59. struct TheClock      *TheClock;
  60.  
  61. // here we go ...
  62.  
  63. void
  64. main(argc, argv)
  65. int argc;
  66. char *argv[];
  67. {
  68.    register struct Process *ThatsMe = (struct Process *)FindTask(NULL);
  69.    int DefFlag = 39; // display time, date, online time and mem
  70.    int i, quit=0, help=0;
  71.    BPTR StdOut = (BPTR)Open("*", MODE_OLDFILE);
  72.  
  73.  
  74.       // open libraries
  75.  
  76.    if(IntuitionBase = OpenLibrary("intuition.library",0)) {
  77.       if(DosBase = OpenLibrary("dos.library",0)) {
  78.  
  79.          // Is the TheClock structure anywhere?
  80.  
  81.          if(TheClock = (struct TheClock *)FindPort(PORTNAME)) DefFlag = TheClock->Flags;
  82.          for (i = 1; i < argc; i++) {
  83.             if(!strcmp(argv[i], "quit") || !strcmp(argv[i], "-q")) {
  84.                quit = 1;
  85.                break;
  86.             }
  87.             else if(!strcmp(argv[i], "-t")) { DefFlag = (DefFlag &  1) ? (DefFlag & 62):(DefFlag |  1); }
  88.             else if(!strcmp(argv[i], "-d")) { DefFlag = (DefFlag &  2) ? (DefFlag & 61):(DefFlag |  2); }
  89.             else if(!strcmp(argv[i], "-m")) { DefFlag = (DefFlag &  4) ? (DefFlag & 59):(DefFlag |  4); }
  90.             else if(!strcmp(argv[i], "-e")) { DefFlag = (DefFlag &  8) ? (DefFlag & 55):(DefFlag |  8); }
  91.             else if(!strcmp(argv[i], "-r")) { DefFlag = (DefFlag & 16) ? (DefFlag & 47):(DefFlag | 16); }
  92.             else if(!strcmp(argv[i], "-o")) { DefFlag = (DefFlag & 32) ? (DefFlag & 31):(DefFlag | 32); }
  93.             else { help = 1; break; }
  94.          }
  95.          if(help && StdOut) {
  96.              WriteStdOut("\33[1mMSClock V1.3\33[0m © 12.02.91 by Martin Steppler\n");
  97.              WriteStdOut("USAGE:  MSClock [-t] [-d] [-m] [-e] [-r] [-q] [quit]\n");
  98.              WriteStdOut("        -t    time   off/on                default: on\n");
  99.              WriteStdOut("        -d    date   off/on                default: on\n");
  100.              WriteStdOut("        -m    memory off/on                default: on\n");
  101.              WriteStdOut("        -o    online time off/on           default: on\n");
  102.              WriteStdOut("        -e    English weekdays on/off      default: German on\n");
  103.              WriteStdOut("        -r    reverse date display on/off  default: off\n");
  104.              WriteStdOut("        -q    quit MSClock\n");
  105.          }
  106.          else if(!TheClock && !quit) {
  107.  
  108.                      // No, then Create Process
  109.  
  110.             if(TheClock = (struct TheClock *)AllocMem(sizeof(struct TheClock),MEMF_PUBLIC | MEMF_CLEAR)) {
  111.  
  112.                      // Dummy MessagePort.
  113.  
  114.                TheClock -> Port . mp_Flags         = PA_IGNORE;
  115.                TheClock -> Port . mp_Node . ln_Pri = 0;
  116.                TheClock -> Port . mp_Node . ln_Type= NT_MSGPORT;
  117.                TheClock -> Port . mp_Node . ln_Name= AllocMem(sizeof(PORTNAME),MEMF_PUBLIC);
  118.                TheClock -> Child                   = NULL;
  119.                TheClock -> Father                  = (struct Task *)ThatsMe;
  120.                TheClock -> SegSize                 = sizeof(struct TheClock);
  121.                TheClock -> Width                   = GetWidth(DefFlag);
  122.                TheClock -> LeftEdge                = GetOverscan() - TheClock->Width;
  123.                TheClock -> Online                  = FALSE;
  124.                TheClock -> Flags                   = DefFlag;
  125.                TheClock -> FlagsBackup             = DefFlag;
  126.  
  127.                      // Init port.
  128.  
  129.                strcpy(TheClock -> Port . mp_Node . ln_Name,PORTNAME);
  130.                NewList(&TheClock -> Port . mp_MsgList);
  131.  
  132.                      // Load the handler code.
  133.  
  134.                if(!(TheClock -> Segment = LoadSeg("MSClock-Handler")))
  135.                   TheClock -> Segment = LoadSeg("L:MSClock-Handler");
  136.                if(!TheClock -> Segment)
  137.                {
  138.                   FreeMem(TheClock -> Port . mp_Node . ln_Name,sizeof(PORTNAME));
  139.                   FreeMem(TheClock,TheClock -> SegSize);
  140.                   if(StdOut)  WriteStdOut("Installation of \33[1mMartin Steppler's Clock\33[0m failed. Don't panic!\n");
  141.                }
  142.                else
  143.                {
  144.                      // Install the port and start the handler.
  145.  
  146.                   AddPort(&TheClock -> Port);
  147.                   CreateProc("Martin's Clock", 5,TheClock -> Segment,4096);
  148.  
  149.                      // Wait for child task to ring back...
  150.  
  151.                   Wait(CHILD_READY);
  152.                   if(!TheClock -> Child)  // failed
  153.                   {
  154.                      RemPort(&TheClock -> Port);
  155.                      FreeMem(TheClock -> Port . mp_Node . ln_Name,sizeof(PORTNAME));
  156.                      if(TheClock -> Segment) UnLoadSeg(TheClock -> Segment);
  157.                      FreeMem(TheClock,TheClock -> SegSize);
  158.                      if(StdOut)  WriteStdOut("Installation of \33[1mMartin Steppler's Clock\33[0m failed. Don't panic!\n");
  159.                   }
  160.                   else             // Yeah, it works
  161.                      if(StdOut)  WriteStdOut("It is my pleasure to install \33[1mMartin Steppler's Clock\33[0m for you!\n");
  162.                }
  163.             }
  164.          }
  165.          else if(TheClock && quit) {
  166.             TheClock -> Father = (struct Task *)ThatsMe;
  167.             Signal(TheClock -> Child,SIG_CLOSE);
  168.             Wait(CHILD_READY);
  169.  
  170.                // Remove port and associated data.
  171.  
  172.             RemPort(&TheClock -> Port);
  173.             FreeMem(TheClock -> Port . mp_Node . ln_Name,sizeof(PORTNAME));
  174.             if(TheClock -> Segment) UnLoadSeg(TheClock -> Segment);
  175.             FreeMem(TheClock,TheClock -> SegSize);
  176.             if(StdOut) {
  177.                WriteStdOut("It is my satisfaction to close \33[1mMartin Steppler's Clock\33[0m again\n");
  178.                WriteStdOut("with the knowledge of a job well done!\n");
  179.             }
  180.          }
  181.          else if(TheClock) {
  182.  
  183.             // store changes and signal child
  184.  
  185.             TheClock->FlagsBackup = DefFlag;
  186.             TheClock->Width = GetWidth(DefFlag);
  187.             TheClock->LeftEdge = GetOverscan() - TheClock->Width;
  188.             TheClock -> Father = (struct Task *)ThatsMe;
  189.             Signal(TheClock -> Child,SIG_CHANGE);
  190.             Wait(CHILD_READY);
  191.          }
  192.       }
  193.    }
  194.    if(StdOut)          Close(StdOut);
  195.    if(IntuitionBase)   CloseLibrary(IntuitionBase);
  196.    if(DosBase)         CloseLibrary(DosBase);
  197. }
  198.  
  199. // calculate width of our window
  200.  
  201. GetWidth(int DefFlag)
  202. {
  203.    int Width = 0;
  204.  
  205.    if(DefFlag &  1) Width+= TIME_WIDTH;
  206.    if(DefFlag &  2) Width+= DATE_WIDTH;
  207.    if(DefFlag &  4) Width+= MEM_WIDTH;
  208.    if(DefFlag & 32) Width+= ONLINE_WIDTH;
  209.  
  210.    // default width
  211.  
  212.    if(!Width) Width = TIME_WIDTH;
  213.  
  214.    return(Width);
  215. }
  216. GetOverscan(void)
  217. {
  218.    register struct Screen *WBench;
  219.    register ULONG IntuiLock;
  220.  
  221.            // Start with the first one.
  222.  
  223.    IntuiLock = LockIBase(0L);
  224.    WBench = IntuitionBase -> FirstScreen;
  225.    UnlockIBase(IntuiLock);
  226.  
  227.            // Scan the list...
  228.    do
  229.    {
  230.            // The type we want?
  231.  
  232.       if((WBench -> Flags & SCREENTYPE) == WBENCHSCREEN)
  233.          return(WBench -> Width - FRONTBACKGAD_WIDTH);
  234.    }
  235.    while(WBench = WBench -> NextScreen);
  236.  
  237.            // Failed!
  238.  
  239.    return(640 - FRONTBACKGAD_WIDTH);
  240. }
  241.